using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Diagnostics;
namespace Common.DS
{
	[HelpKeyword("vs.data.TableAdapterManager"), Designer("Microsoft.VSDesigner.DataSource.Design.TableAdapterManagerDesigner, Microsoft.VSDesigner, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"), DesignerCategory("code"), ToolboxItem(true)]
	public class TableAdapterManager : Component
	{
		[GeneratedCode("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
		public enum UpdateOrderOption
		{
			InsertUpdateDelete,
			UpdateInsertDelete
		}
		[GeneratedCode("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
		private class SelfReferenceComparer : IComparer<DataRow>
		{
			private DataRelation _relation;
			private int _childFirst;
			[GeneratedCode("System.Data.Design.TypedDataSetGenerator", "4.0.0.0"), DebuggerNonUserCode]
			internal SelfReferenceComparer(DataRelation relation, bool childFirst)
			{
				this._relation = relation;
				if (childFirst)
				{
					this._childFirst = -1;
				}
				else
				{
					this._childFirst = 1;
				}
			}
			[GeneratedCode("System.Data.Design.TypedDataSetGenerator", "4.0.0.0"), DebuggerNonUserCode]
			private DataRow GetRoot(DataRow row, out int distance)
			{
				Debug.Assert(row != null);
				DataRow result = row;
				distance = 0;
				IDictionary<DataRow, DataRow> dictionary = new Dictionary<DataRow, DataRow>();
				dictionary[row] = row;
				DataRow parentRow = row.GetParentRow(this._relation, DataRowVersion.Default);
				while (parentRow != null && !dictionary.ContainsKey(parentRow))
				{
					distance++;
					result = parentRow;
					dictionary[parentRow] = parentRow;
					parentRow = parentRow.GetParentRow(this._relation, DataRowVersion.Default);
				}
				if (distance == 0)
				{
					dictionary.Clear();
					dictionary[row] = row;
					parentRow = row.GetParentRow(this._relation, DataRowVersion.Original);
					while (parentRow != null && !dictionary.ContainsKey(parentRow))
					{
						distance++;
						result = parentRow;
						dictionary[parentRow] = parentRow;
						parentRow = parentRow.GetParentRow(this._relation, DataRowVersion.Original);
					}
				}
				return result;
			}
			[GeneratedCode("System.Data.Design.TypedDataSetGenerator", "4.0.0.0"), DebuggerNonUserCode]
			public int Compare(DataRow row1, DataRow row2)
			{
				int result;
				if (object.ReferenceEquals(row1, row2))
				{
					result = 0;
				}
				else
				{
					if (row1 == null)
					{
						result = -1;
					}
					else
					{
						if (row2 == null)
						{
							result = 1;
						}
						else
						{
							int num = 0;
							DataRow root = this.GetRoot(row1, out num);
							int value = 0;
							DataRow root2 = this.GetRoot(row2, out value);
							if (object.ReferenceEquals(root, root2))
							{
								result = this._childFirst * num.CompareTo(value);
							}
							else
							{
								Debug.Assert(root.Table != null && root2.Table != null);
								if (root.Table.Rows.IndexOf(root) < root2.Table.Rows.IndexOf(root2))
								{
									result = -1;
								}
								else
								{
									result = 1;
								}
							}
						}
					}
				}
				return result;
			}
		}
		private TableAdapterManager.UpdateOrderOption _updateOrder;
		private STOCKTableAdapter _sTOCKTableAdapter;
		private bool _backupDataSetBeforeUpdate;
		private IDbConnection _connection;
		[GeneratedCode("System.Data.Design.TypedDataSetGenerator", "4.0.0.0"), DebuggerNonUserCode]
		public TableAdapterManager.UpdateOrderOption UpdateOrder
		{
			get
			{
				return this._updateOrder;
			}
			set
			{
				this._updateOrder = value;
			}
		}
		[GeneratedCode("System.Data.Design.TypedDataSetGenerator", "4.0.0.0"), Editor("Microsoft.VSDesigner.DataSource.Design.TableAdapterManagerPropertyEditor, Microsoft.VSDesigner, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor"), DebuggerNonUserCode]
		public STOCKTableAdapter STOCKTableAdapter
		{
			get
			{
				return this._sTOCKTableAdapter;
			}
			set
			{
				this._sTOCKTableAdapter = value;
			}
		}
		[GeneratedCode("System.Data.Design.TypedDataSetGenerator", "4.0.0.0"), DebuggerNonUserCode]
		public bool BackupDataSetBeforeUpdate
		{
			get
			{
				return this._backupDataSetBeforeUpdate;
			}
			set
			{
				this._backupDataSetBeforeUpdate = value;
			}
		}
		[GeneratedCode("System.Data.Design.TypedDataSetGenerator", "4.0.0.0"), Browsable(false), DebuggerNonUserCode]
		public IDbConnection Connection
		{
			get
			{
				IDbConnection result;
				if (this._connection != null)
				{
					result = this._connection;
				}
				else
				{
					if (this._sTOCKTableAdapter != null && this._sTOCKTableAdapter.Connection != null)
					{
						result = this._sTOCKTableAdapter.Connection;
					}
					else
					{
						result = null;
					}
				}
				return result;
			}
			set
			{
				this._connection = value;
			}
		}
		[GeneratedCode("System.Data.Design.TypedDataSetGenerator", "4.0.0.0"), Browsable(false), DebuggerNonUserCode]
		public int TableAdapterInstanceCount
		{
			get
			{
				int num = 0;
				if (this._sTOCKTableAdapter != null)
				{
					num++;
				}
				return num;
			}
		}
		[GeneratedCode("System.Data.Design.TypedDataSetGenerator", "4.0.0.0"), DebuggerNonUserCode]
		private int UpdateUpdatedRows(dsSTOCK dataSet, List<DataRow> allChangedRows, List<DataRow> allAddedRows)
		{
			int num = 0;
			if (this._sTOCKTableAdapter != null)
			{
				DataRow[] array = dataSet.STOCK.Select(null, null, DataViewRowState.ModifiedCurrent);
				array = this.GetRealUpdatedRows(array, allAddedRows);
				if (array != null && 0 < array.Length)
				{
					num += this._sTOCKTableAdapter.Update(array);
					allChangedRows.AddRange(array);
				}
			}
			return num;
		}
		[GeneratedCode("System.Data.Design.TypedDataSetGenerator", "4.0.0.0"), DebuggerNonUserCode]
		private int UpdateInsertedRows(dsSTOCK dataSet, List<DataRow> allAddedRows)
		{
			int num = 0;
			if (this._sTOCKTableAdapter != null)
			{
				DataRow[] array = dataSet.STOCK.Select(null, null, DataViewRowState.Added);
				if (array != null && 0 < array.Length)
				{
					num += this._sTOCKTableAdapter.Update(array);
					allAddedRows.AddRange(array);
				}
			}
			return num;
		}
		[GeneratedCode("System.Data.Design.TypedDataSetGenerator", "4.0.0.0"), DebuggerNonUserCode]
		private int UpdateDeletedRows(dsSTOCK dataSet, List<DataRow> allChangedRows)
		{
			int num = 0;
			if (this._sTOCKTableAdapter != null)
			{
				DataRow[] array = dataSet.STOCK.Select(null, null, DataViewRowState.Deleted);
				if (array != null && 0 < array.Length)
				{
					num += this._sTOCKTableAdapter.Update(array);
					allChangedRows.AddRange(array);
				}
			}
			return num;
		}
		[GeneratedCode("System.Data.Design.TypedDataSetGenerator", "4.0.0.0"), DebuggerNonUserCode]
		private DataRow[] GetRealUpdatedRows(DataRow[] updatedRows, List<DataRow> allAddedRows)
		{
			DataRow[] result;
			if (updatedRows == null || updatedRows.Length < 1)
			{
				result = updatedRows;
			}
			else
			{
				if (allAddedRows == null || allAddedRows.Count < 1)
				{
					result = updatedRows;
				}
				else
				{
					List<DataRow> list = new List<DataRow>();
					for (int i = 0; i < updatedRows.Length; i++)
					{
						DataRow item = updatedRows[i];
						if (!allAddedRows.Contains(item))
						{
							list.Add(item);
						}
					}
					result = list.ToArray();
				}
			}
			return result;
		}
		[GeneratedCode("System.Data.Design.TypedDataSetGenerator", "4.0.0.0"), DebuggerNonUserCode]
		public virtual int UpdateAll(dsSTOCK dataSet)
		{
			if (dataSet == null)
			{
				throw new ArgumentNullException("dataSet");
			}
			int result;
			if (!dataSet.HasChanges())
			{
				result = 0;
			}
			else
			{
				if (this._sTOCKTableAdapter != null && !this.MatchTableAdapterConnection(this._sTOCKTableAdapter.Connection))
				{
					throw new ArgumentException("All TableAdapters managed by a TableAdapterManager must use the same connection string.");
				}
				IDbConnection connection = this.Connection;
				if (connection == null)
				{
					throw new ApplicationException("TableAdapterManager contains no connection information. Set each TableAdapterManager TableAdapter property to a valid TableAdapter instance.");
				}
				bool flag = false;
				if ((connection.State & ConnectionState.Broken) == ConnectionState.Broken)
				{
					connection.Close();
				}
				if (connection.State == ConnectionState.Closed)
				{
					connection.Open();
					flag = true;
				}
				IDbTransaction dbTransaction = connection.BeginTransaction();
				if (dbTransaction == null)
				{
					throw new ApplicationException("The transaction cannot begin. The current data connection does not support transactions or the current state is not allowing the transaction to begin.");
				}
				List<DataRow> list = new List<DataRow>();
				List<DataRow> list2 = new List<DataRow>();
				List<DataAdapter> list3 = new List<DataAdapter>();
				Dictionary<object, IDbConnection> dictionary = new Dictionary<object, IDbConnection>();
				int num = 0;
				DataSet dataSet2 = null;
				if (this.BackupDataSetBeforeUpdate)
				{
					dataSet2 = new DataSet();
					dataSet2.Merge(dataSet);
				}
				try
				{
					if (this._sTOCKTableAdapter != null)
					{
						dictionary.Add(this._sTOCKTableAdapter, this._sTOCKTableAdapter.Connection);
						this._sTOCKTableAdapter.Connection = (SqlConnection)connection;
						this._sTOCKTableAdapter.Transaction = (SqlTransaction)dbTransaction;
						if (this._sTOCKTableAdapter.Adapter.AcceptChangesDuringUpdate)
						{
							this._sTOCKTableAdapter.Adapter.AcceptChangesDuringUpdate = false;
							list3.Add(this._sTOCKTableAdapter.Adapter);
						}
					}
					if (this.UpdateOrder == TableAdapterManager.UpdateOrderOption.UpdateInsertDelete)
					{
						num += this.UpdateUpdatedRows(dataSet, list, list2);
						num += this.UpdateInsertedRows(dataSet, list2);
					}
					else
					{
						num += this.UpdateInsertedRows(dataSet, list2);
						num += this.UpdateUpdatedRows(dataSet, list, list2);
					}
					num += this.UpdateDeletedRows(dataSet, list);
					dbTransaction.Commit();
					if (0 < list2.Count)
					{
						DataRow[] array = new DataRow[list2.Count];
						list2.CopyTo(array);
						for (int i = 0; i < array.Length; i++)
						{
							DataRow dataRow = array[i];
							dataRow.AcceptChanges();
						}
					}
					if (0 < list.Count)
					{
						DataRow[] array = new DataRow[list.Count];
						list.CopyTo(array);
						for (int i = 0; i < array.Length; i++)
						{
							DataRow dataRow = array[i];
							dataRow.AcceptChanges();
						}
					}
				}
				catch (Exception ex)
				{
					dbTransaction.Rollback();
					if (this.BackupDataSetBeforeUpdate)
					{
						Debug.Assert(dataSet2 != null);
						dataSet.Clear();
						dataSet.Merge(dataSet2);
					}
					else
					{
						if (0 < list2.Count)
						{
							DataRow[] array = new DataRow[list2.Count];
							list2.CopyTo(array);
							for (int i = 0; i < array.Length; i++)
							{
								DataRow dataRow = array[i];
								dataRow.AcceptChanges();
								dataRow.SetAdded();
							}
						}
					}
					throw ex;
				}
				finally
				{
					if (flag)
					{
						connection.Close();
					}
					if (this._sTOCKTableAdapter != null)
					{
						this._sTOCKTableAdapter.Connection = (SqlConnection)dictionary[this._sTOCKTableAdapter];
						this._sTOCKTableAdapter.Transaction = null;
					}
					if (0 < list3.Count)
					{
						DataAdapter[] array2 = new DataAdapter[list3.Count];
						list3.CopyTo(array2);
						for (int i = 0; i < array2.Length; i++)
						{
							DataAdapter dataAdapter = array2[i];
							dataAdapter.AcceptChangesDuringUpdate = true;
						}
					}
				}
				result = num;
			}
			return result;
		}
		[GeneratedCode("System.Data.Design.TypedDataSetGenerator", "4.0.0.0"), DebuggerNonUserCode]
		protected virtual void SortSelfReferenceRows(DataRow[] rows, DataRelation relation, bool childFirst)
		{
			Array.Sort<DataRow>(rows, new TableAdapterManager.SelfReferenceComparer(relation, childFirst));
		}
		[GeneratedCode("System.Data.Design.TypedDataSetGenerator", "4.0.0.0"), DebuggerNonUserCode]
		protected virtual bool MatchTableAdapterConnection(IDbConnection inputConnection)
		{
			return this._connection != null || (this.Connection == null || inputConnection == null) || string.Equals(this.Connection.ConnectionString, inputConnection.ConnectionString, StringComparison.Ordinal);
		}
	}
}
